package com.example;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.database.CurriculumMessage;
import com.example.database.MyCourseMessage;
import com.example.entity.Curriculum;
import com.example.view.Customize;
import com.example.view.CustomizeBottom;

import java.util.ArrayList;
import java.util.List;

public class CurriculumActivity extends AppCompatActivity {
    private Customize customize ;
    private CustomizeBottom customizeBottom ;
    private CurriculumMessage curriculumMessage ;
    private MyCourseMessage myCourseMessage ;
    private CurriculumAdapter adapter ;
    private List<String[]> curriculumInfoList ;
    private ListView listView ;
    private ArrayList<Curriculum> curriculumList ;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.array);
        customize = findViewById(R.id.student_top);
        customizeBottom = findViewById(R.id.student_bottom);
        customize.setLeftBtnNoVisible(true);//不显示
        customize.setRightBtnNoVisible(true);
        customize.setTitleText("课程");
        customizeBottom.setCourseBtnBackground(R.color.bottomUse);

        listView = findViewById(R.id.listView);

        //拿到登录界面的用户Id
        SharedPreferences sharedPreferences = getSharedPreferences("loginIdAndName" , Context.MODE_PRIVATE);
        String studentId = sharedPreferences.getString("studentId", "");

        curriculumMessage = new CurriculumMessage(this);
        myCourseMessage = new MyCourseMessage(this);
        List<String> curriculumIds = myCourseMessage.getChosenCurriculumIds(studentId);
        Log.d("", "--------------------------------"+curriculumIds+"-------------------------------");
        curriculumInfoList = new ArrayList<>();
        for (String curriculumId : curriculumIds) {
            String curriculumName = curriculumMessage.getCourseName(curriculumId);
            Log.d("", "--------------------------------"+curriculumName+"-------------------------------");
            curriculumInfoList.add(new String[]{curriculumId, curriculumName});
        }

        adapter = new CurriculumAdapter(this, curriculumInfoList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView getId = view.findViewById(R.id.tv_curriculum_id);
                Log.d("", "+++++++++++++++++++++"+getId+"++++++++++++++++");
                String curriculumId = getId.getText().toString() ;
                Curriculum curriculum = curriculumMessage.getCourseDetails(curriculumId);

                // 显示课程详细信息的对话框
                showCurriculumDetailsDialog(curriculum);
            }
        });

        customizeBottom.setStudentBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CurriculumActivity.this , StudentActivity.class) ;
                startActivity(intent);
                finish();
            }
        });
        customizeBottom.setPersonBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CurriculumActivity.this , PersonalActivity.class) ;
                startActivity(intent);
                finish();
            }
        });

    }

    private void showCurriculumDetailsDialog(Curriculum curriculum) {
        if (curriculum != null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(CurriculumActivity.this);
            builder.setTitle("课程信息");
            builder.setMessage("课程ID: " + curriculum.getCurriculumName() + "\n"
                    + "课程名称: " + curriculum.getCurriculumId() + "\n"
                    + "授课教师: " + curriculum.getCurriculumTeacher() + "\n"
                    + "上课班级: " + curriculum.getCurriculumClass() + "\n"
                    + "上课时间: " + curriculum.getCurriculumTime()+"学时");
            builder.setPositiveButton("确定", null);
            AlertDialog dialog = builder.create();
            dialog.show();
        }}
}
